/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.tzconverter.menu.Item;
import org.tzconverter.menu.MenuView;

/**
 * Base class for all of the managers.  This base class provides common
 * functionality implemented by most, if not all, of the managers.  For
 * example, this abstract class will register the manager to receive
 * save menu events.
 * 
 * 
 * @since Jun 20, 2009
 */

public abstract class AbstractManager
{
  /**
   * The menu view for all of the managers.
   */
  protected final MenuView menuView;
  
  private ActionListener saveListener;
  private ActionListener loadListener;
  private ActionListener loadDefaultsListener;
  
  public AbstractManager(MenuView menuView)
  {
    this.menuView = menuView;
    registerListeners();
  }
  
  /**
   * Called when the save settings menu item is clicked.  The default
   * implementation does nothing.
   */
  protected void whenSaveClicked()
  {
    //Default implementation does nothing.
  }

  /**
   * Called when the load settings menu item is clicked.  The default
   * implementation does nothing.
   */  
  protected void whenLoadClicked()
  {
    
  }
  
  /**
   * Called when the Load Defaults menu item is clicked.  The
   * default implementation does nothing.
   */
  protected void whenLoadDefaultsClicked()
  {
    
  }
  
  private void registerListeners()
  {
    saveListener = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        whenSaveClicked();
      }
    };  
    menuView.addActionListener(Item.SAVE_SETTINGS, saveListener);
    
    loadListener = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        whenLoadClicked();
      }      
    };
    menuView.addActionListener(Item.LOAD_SETTINGS, loadListener);
    
    loadDefaultsListener = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        whenLoadDefaultsClicked();
      }      
    };
    menuView.addActionListener(Item.LOAD_DEFAULTS, loadDefaultsListener);      
  }
  
  protected void unregisterListeners()
  {
    menuView.removeActionListener(Item.SAVE_SETTINGS, saveListener);
    menuView.removeActionListener(Item.LOAD_SETTINGS, loadListener);
    menuView.removeActionListener(Item.LOAD_DEFAULTS, loadDefaultsListener);
  }
}
